<?php
//this model is using for states....
class Cities_model extends Model{
	
	function Cities_model()
	{
		parent::Model();
		$this->load->database();
	}
	
	//to insert cities in bulk...
	function insert_cities($files='')
	{
	
	require_once 'excel_reader2.php';
    $xls = new Spreadsheet_Excel_Reader('./district_uploads/'.$files);
    
    
    $cities = $xls->dump_cities();
	
	foreach($cities as $city)
	{
		$check = $this->db->query("select * from tbl_city where city_name like '".$city['districts']."' AND state_id='".$_POST['state']."'");
		
		if($check->num_rows() == 0)
		{
			$this->db->set('city_name',$city['districts']);
			$this->db->set('state_id',$_POST['state']);
			$this->db->insert('tbl_city');	
		}
	}
    
   
    
		
			
	}
	//to insert new city...
	function insert_city($state_id,$new_city)
	{
	//print_r($_POST);
		$check = $this->db->query("select * from tbl_city where city_name like '".$new_city."' AND state_id='".$state_id."'");
		
		if($check->num_rows() == 0)
		{
			$this->db->set('city_name',$new_city);
			$this->db->set('state_id',$state_id);
			$this->db->insert('tbl_city');	
      return 1;
		}
		else
		{
			return 0;
		}
		
	}
  
  function get_statename($id=0)
  {
    $result = $this->db->query("select * from tbl_states where state_id=".$id);
    return $result;
  }
  
  public function get_cities($id=0) 
  {
    $this->CI =& get_instance();
    
    $this->db->select('* from tbl_city where state_id ='.$id);
    $this->CI->flexigrid->build_query();
    
    //Get contents
    $return['records'] = $this->db->get();
    
    //Build count query
    $this->db->select('count(city_id) as record_count from tbl_city where state_id= '.$id);
    $this->CI->flexigrid->build_query(FALSE);
    $record_count = $this->db->get();
    $row = $record_count->row();
    
    //Get Record Count
    $return['record_count'] = $row->record_count;
  
    //Return all
    return $return;
  }
  
	//to count no of result set.....
	function cities_count()
	{
		$result = $this->db->query("select * from tbl_city order by city_name ASC");
		return $result->num_rows();
	}
	
	function cities_list($id)
	{
		$name = $this->db->query("select * from tbl_city where state_id='".$id."' order by city_name ASC ");
		return $name;
	}
	
	//to display cities...
	/*function cities_list($num,$limitnum)
	{
		$name = $this->db->query("select * from tbl_city order by city_name ASC limit ".$num." , ".$limitnum);
		return $name;
	}
*/	//to fetch city name to edit...
	function city_edit($id)
	{
		$result = $this->db->query("select * from tbl_city where city_id =".$id);
		return $result;
	}
	//to save edited city name
	function save_edit()
	{
		//print_r($_POST);
		$data = array(
               'city_name' => $_POST['city_name'],
            );

	$this->db->where('city_id', $_POST['id']);
	$this->db->update('tbl_city', $data); 
	}
	//to display cities in ajax...
	
	function disp_cities($id=0)
	{
		
		$result = $this->db->query("select * from tbl_city where state_id =".$id);
		return $result;
	}
	
}
